//
//  WRYAdvertisingController.m
//  BaiSiBuJie
//
//  Created by wry on 16/4/27.
//  Copyright © 2016年 WRY. All rights reserved.
//

#import "WRYAdvertisingController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "WRYAdvertisingItem.h"
#import <UIImageView+WebCache.h>

#import "WRYTabBarController.h"

#define WRYCode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
@interface WRYAdvertisingController ()
@property (weak, nonatomic) IBOutlet UIImageView *lunchImageView;//启动图片
@property (weak, nonatomic) IBOutlet UIButton *skipBtn;//跳过按钮
@property (weak, nonatomic) IBOutlet UIImageView *advertisingImageView;//广告图片

@property(strong,nonatomic)NSArray *adItems;
@property(strong,nonatomic)NSTimer *timer;

@end

@implementation WRYAdvertisingController
/**
 1.为什么要在广告界面展示启动图片，让用户一直觉得程序在启动的过程中
   占位视图：先搞个view占着这个位置，等view确定了之后，就往占位视图添加了
 2.当一个view的层次结构，确定了之后，但是里面具体子控件还不确定，可以使用占位视图思想.
 3.启动图片都可以使用，直接往项目中导入.
 4.根据不同屏幕尺寸，去加载不同图片
 */

- (IBAction)skipBtnClick:(UIButton *)sender {
    //跳到主界面
    [UIApplication sharedApplication].keyWindow.rootViewController=[[WRYTabBarController alloc]init];
    //销毁定时器
    [self.timer invalidate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.展示启动图片
    [self setUpLaunchScreenImage];
    //2.获取广告数据
    [self loadAdvertisingData];
    //3.定时减一
    NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    _timer=timer;
    
}


//1.展示启动图片
-(void)setUpLaunchScreenImage
{
    //屏幕适配:根据不同屏幕尺寸，去加载不同图片
    if (iPhone6P) {
        _lunchImageView.image=[UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }else if (iPhone6)
    {_lunchImageView.image=[UIImage imageNamed:@"LaunchImage-800-667h"];}
    else if (iPhone5){
        _lunchImageView.image=[UIImage imageNamed:@"LaunchImage-568h"];
    }
    else if (iPhone4){
        _lunchImageView.image=[UIImage imageNamed:@"LaunchImage"];
    }
}

//2.获取广告数据
// 请求数据 -》 查看接口文档（1.基本url 2.请求参数 3.请求方式） -》 AFN -> 解析数据 -》 设计模型 -》 字典转模型 -》 把模型的内容展示到控件上
-(void)loadAdvertisingData
{
    
    /**
     (w_picurl:广告界面,ori_curl:点击广告界面进入网页,w,h)
     */
    
    
    // "Request failed: unacceptable content-type: text/html",需要设置响应头格式
    // 1.创建请求会话管理者
    AFHTTPSessionManager*manager=[AFHTTPSessionManager manager];
    
    // 2.1创建请求参数字典
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = WRYCode2;

    // 2.发送请求 get,post 查看接口文档
[manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSDictionary *adDict = [responseObject[@"ad"] firstObject];
    
    //2.2解析数据
    WRYAdvertisingItem *item=[WRYAdvertisingItem mj_objectWithKeyValues:adDict];
    //2.3把模型的内容展示到控件上
    CGFloat h=WRYScreenW/item.w*item.h;
    UIImageView *adView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WRYScreenW,h)];
    [adView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
    [_advertisingImageView addSubview:adView];

    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    NSLog(@"%@",error);
}];

}

//3.定时减一
-(void)timeChange
{
    //每1s数字-1
    // 按钮文字 如果一闪一闪 就是按钮的样式不对，是系统system样式上,要改为custom样式
    static int i=3;
    i--;
    [self.skipBtn setTitle:[NSString stringWithFormat:@"跳过(%d)",i] forState:(UIControlStateNormal)];
    
    if (i<0) {
        [self skipBtnClick:self.skipBtn];
    }
}


@end
